﻿using SuperX.Common.Log;
using System;
using System.Collections.Generic;
using System.Text;

namespace SuperX.Common.OS
{
    [Command("Ubuntu", "Debian GNU/Linux")]
    public class UbuntuCommand : LinuxCommand
    {
        private string _prettyName;


        public UbuntuCommand()
        {

        }

        private string DetectDebInstalledCommand(string debName)
        {
            var detectCommand = $"dpkg -l | grep \"{debName }\"";
            return detectCommand;
        }

        public override bool IsSupportIPManager()
        {
            var command = DetectDebInstalledCommand("network-manager");
            var result = ScriptExecute(command);
            Logger.Trace(command + Environment.NewLine + result);
            return !string.IsNullOrWhiteSpace(result);
        }

        public override FunctionState GetSSHServerState()
        {
            var command = "systemctl status ssh";
            var result = ScriptExecute(command);
            Logger.Trace(command + Environment.NewLine + result);
            var lines = result.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length >= 3 && lines[2].Contains("dead")) return FunctionState.NotStart;
            if (lines.Length >= 3 && lines[2].Contains("running")) return FunctionState.Running;
            return FunctionState.NotFound;
        }

        public override void RestartSSHServer()
        {
            var command = "systemctl restart ssh";
            var result = ScriptExecute(command);
            Logger.Trace(command + Environment.NewLine + result);
        }
    }
}
